// mircoApp/customCluster/takeList/takeList.js
let app = getApp();
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    active:'',
    tabList:[
      {
        name:'已抢购',
        status:" "
      },
      {
        name:'待发放',
        status:0
      },
      {
        name:'已完成',
        status:1
      },
      // {
      //   name:'定制失败',
      //   status:2
      // }
    ],
    categoryTitle:'请选择分类',
    menuClass:false,
    // categoryName:'',
    page: 1,
    isLoadMore: true,
    total_page: 0,
    groupList:[],
    categoryList:[{
      id:'',
      category_name:'全部'
    }],
    categoryId:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      page:1,
      categoryList:[{
        id:'',
        category_name:'全部'
      }]
    })
    this.polyIndex();
    
  },
  categoryClick() {
    this.setData({
      menuClass:!this.data.menuClass
    })
  },
  takeTab(event) {
    let name = event.detail.name;
    this.setData({
      active:name,
      groupList:[],
      isLoadMore:true,
      page:1
    })
    this.groupData();
  },
  categoryOn(event) {
    let item = event.currentTarget.dataset.item;
    this.setData({
      categoryId:item.id,
      categoryTitle:item.category_name,
      page:1,
      menuClass:false
    })
    this.groupData();
  },
  toDetail(event) {
    let order = event.currentTarget.dataset.order;
    wx.navigateTo({
      url: `/packageA/member/orderdetail/orderdetail?order_id=${order.id}&orderType=customized-group`
    });
  },
  polyIndex() { //会员信息
    let urlStr = app.getNetAddresss("plugin.customized-group.frontend.controllers.index.index");
    app._postNetWork({
      url: urlStr,
      data: {basic_info:1},
      success: (resdata) => {
        var res = resdata.data;
        let data = res.data;
        if (res.result == 1) {
          let category = data.category;
          this.setData({
            categoryList:[...this.data.categoryList,...category]
          })
          this.groupData();
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  groupData() {
    let query = {
      page:this.data.page,
      category_id:this.data.categoryId,
      status:this.data.active
    };
    let urlStr = app.getNetAddresss("plugin.customized-group.frontend.controllers.group.join-group");
    app._postNetWork({
      url: urlStr,
      data:query,
      success:  (resdata) => {
        let res = resdata.data;
        let data = res.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.totalPage = data.list?.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          let groupList = data.list.data;
          this.setData({
            groupList: groupList
          })
        } else {
          this.data.page = this.data.page - 1;
          this.data.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let query = {
        page:this.data.page,
        category_id:this.data.categoryId,
        status:this.data.active
      };
      let urlStr = app.getNetAddresss("plugin.customized-group.frontend.controllers.group.join-group");
      app._postNetWork({
        url: urlStr,
        data: query,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.list?.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              groupList : this.data.groupList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
     title: '刷新中...',
   })
   this.setData({
     page:1,
     groupList:[],
     isLoadMore: true,
   })
   this.groupData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})